<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>返回顶部</title>
    <style>
        body{
            height: 5000px;
            background-image: linear-gradient(to bottom,blue,red,yellow);
        }
        .backtotop{
            width: 60px;
            height: 60px;
            background-color: rgba(255,255,255,.6);
            position: fixed;
            bottom: 100px;
            right: 100px;
            /* 小手状 */
            cursor: pointer;

        }
    </style>
</head>
<body>
    <div class="backtotop" id="backtotopbtn">返回顶部</div>
    <script>
        var backtotopbtn=document.getElementById('backtotopbtn')
        var timer

        backtotopbtn.onclick=function(){
            //设表先关
            clearInterval(timer)

            //设置定时器
            timer=setInterval(function(){
                //不断让scrolltop减少
                document.documentElement.scrollTop-=50;
                //定时器肯定要停
                if(document.documentElement.scrollTop<=0){
                    clearInterval(timer)
                }
            },20)
        }
    </script>
</body>
</html>